<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 样板旁站记录 -->
    <div id="base-breadcrumb" style="position: relative; overflow: hidden">
      <!-- 搜索 -->
      <searchData :searchData="searchArr" @search="search"></searchData>
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- <title-name :title="$route.meta.title"></title-name> -->
        <!-- 表格 -->
        <list-table
          ref="table"
          class="siteRecord-table"
          tableLayout="fixed"
          size="default"
          :columns="columns"
          :request="loadData"
          @select-click="selectClick"
          :isShowCountBar="false"
          :selectConfig="{ use: true, type: 'checkbox', rowKey: 'id' }"
        >
          <div class="mb12" slot="prefix" slot-scope="ref">
            <a-button v-btnPermission="'qm_014_add_02'" icon="plus" type="primary" @click="handleManage(2)"
              >添加旁站</a-button
            >
            <a-button
              v-btnPermission="'qm_014_sexp_05'"
              :loading="exportLoading[0]"
              icon="download"
              class="ml12"
              :disabled="!ref.selectedRowKeys.length"
              type="default"
              @click="exportWord(0, ref.selectedRowKeys)"
              >选中导出</a-button
            >
            <a-button
              v-btnPermission="'qm_014_aexp_06'"
              :loading="exportLoading[1]"
              icon="cloud-download"
              class="ml12"
              type="default"
              @click="exportWord(1)"
              >全部导出</a-button
            >
          </div>
          <!-- 统计汇总 -->
          <AuditSummary
            slot="bottom"
            title="样板工程"
            :form="queryParam"
            :loadCount="loadCount"
            :selectLength="selectLength" 
            :api="api"
            :countList="countList"
          />
          <template slot="action" slot-scope="text, record">
            <MoreButton>
            <a v-btnPermission="'qm_014_loo_01'" @click="handleManage(0, record)">查看</a>
            <template v-if="record.statusCode === '0' || record.statusCode === '3'">
              <a v-btnPermission="'qm_014_edi_03'" @click="handleManage(1, record)">编辑</a>
            </template>
            <template v-if="record.statusCode === '0'">
              <a v-btnPermission="'qm_014_del_04'" @click="handleRemove(record.id)">删除</a>
            </template>
            </MoreButton>
          </template>
          <a-list
            slot="expandedRowRender"
            slot-scope="record"
            item-layout="horizontal"
            :data-source="formatItem(record)"
          >
            <a-list-item slot="renderItem" slot-scope="item">
              <div>{{ item }}</div>
            </a-list-item>
          </a-list>
        </list-table>
      </a-card>
    </div>
  </base-breadcrumb>
</template>

<script>
import { stationRecord } from '@/api/quality/sample' // api
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import OrgTreeSelect from '@/components/OrgTreeSelect'
import BasePage from '@/views/basePage'
import AuditSummary from '@/views/quality/components/AuditSummary' // 统计汇总
import SelectProjectStatus from '@/views/quality/components/SelectProjectStatus' // 项目状态

const searchArr = [
  // 搜索框
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '分公司',
  //   type: SelectSubCompany,
  //   valueKey: 'branchCompanyId'
  // },
  // {
  //   name: '项目名称',
  //   type: SelectProject,
  //   valueKey: 'projectId'
  // },
  {
    name: '项目状态',
    type: SelectProjectStatus,
    valueKey: 'projectStatus'
  },
  {
    name: '施工时间',
    type: 'dateRange',
    value: 'time',
    attr: {
      rangeFormat: ['beginDate', 'endDate']
    }
  },
  {
    name: '旁站人',
    type: 'input',
    value: 'bystander'
  },
  {
    name: '样板部位',
    type: 'input',
    value: 'part'
  },
  {
    name: '样板名称',
    type: 'input',
    value: 'name'
  },
  {
    name: '劳务单位',
    type: 'input',
    value: 'subcontractor'
  }
]
const columns = [
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchCompanyName'
  },
  {
    title: '项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '样板名称',
    ellipsis: true,
    dataIndex: 'name'
  },
  {
    title: '样板部位',
    ellipsis: true,
    dataIndex: 'part'
  },
  {
    title: '旁站人',
    ellipsis: true,
    dataIndex: 'bystander'
  },
  {
    // ellipsis: true,
    title: '施工时间',
    dataIndex: 'constructionDate'
  },
  {
    title: '旁站记录时间',
    // ellipsis: true,
    dataIndex: 'recordDate'
  },
  {
    title: '劳务单位',
    ellipsis: true,
    dataIndex: 'subcontractor'
  },
  {
    title: '状态',
    // width: '80px',
    dataIndex: 'statusName'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    AuditSummary
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      // 查询参数
      queryParam: {},
      // 盒子高度
      boxHeight: 0,
      removeApi: stationRecord,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return stationRecord.postList(requestParameters).then((res) => {
          return res
        })
      },
      editModel: 0,
      selectLength:0,
      api: stationRecord, // 汇总api
      loadCount: 0, // 汇总加载控件
      countList: [
        {
          title: '已选中：',
          value: 'selectLength'
        },
        {
          title: '总数：',
          value: 'total'
        },
        {
          title: '旁站数：',
          value: 'stationNum'
        }
      ], // 汇总列表
      form: {},
      exportLoading: {
        0: false,
        1: false
      } // 导出loading
    }
  },
  methods: {
    selectClick(a,b){
      this.selectLength = a.rows.length;
    },
    /**
     * @description: 导出word
     * @param {*} index：导出类型：0 选中，1 全部
     * @param {*} datas：选中项
     */
    exportWord(index, datas) {
      let args = {}
      if (datas?.length) {
        args.ids = datas
      } else {
        args = this.queryParam
      }
      this.exportLoading[index] = true
      stationRecord
        .batchPrint(args)
        .then((res) => {
          this.exportLoading[index] = false
          if (res.code === 200) {
            // 格式化名称，防止重名被覆盖
            res.data.forEach((i,idx) => {
              i.name = (Number(idx) + 1)  + '.' + i.name
            })
            this.exportPointInfo(res.data, '样板旁站记录')
          }
        })
        .catch(() => {
          this.exportLoading[index] = false
        })
    },
    // 格式化检查项列表
    formatItem(e) {
      let datas = []
      let title = { makeProcess: '样板制作过程：' }
      // let title = { content: '记录内容：', makeProcess: '样板制作过程：' }
      for (let index in e) {
        let item = e[index]
        for (let i in title) {
          if (i == index) {
            datas.push(title[i] + (e[index] || '暂无'))
          }
        }
      }
      return datas
    },
    // 搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
      this.loadCount++
    },
    // 操作栏
    handleManage(type, record) {
      this.editModel = type
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: '/quality/modelProject/siteRecord/siteRecordEdit',
        query
      })
    }
  }
}
</script>
<style lang="less">
.siteRecord-table .ant-table-fixed-right .ant-table-expanded-row {
    height: 64px;
}
</style>
